<?php
class Cache {
    private $cacheDir;
    private $expire;

    public function __construct($expire = 3600) {
        $this->cacheDir = dirname(__DIR__) . '/cache/'; // 缓存目录
        $this->expire = $expire;
        // 确保缓存目录存在
        if (!is_dir($this->cacheDir)) {
            mkdir($this->cacheDir, 0777, true);
        }
    }

    // 获取缓存
    public function get($key) {
        $filename = $this->cacheDir . md5($key) . '.txt';
        if (file_exists($filename) && filemtime($filename) + $this->expire > time()) {
            return unserialize(file_get_contents($filename));
        }
        return false;
    }

    // 设置缓存
    public function set($key, $value) {
        $filename = $this->cacheDir . md5($key) . '.txt';
        return file_put_contents($filename, serialize($value));
    }

    // 删除缓存（关键：确保能彻底删除）
    public function delete($key) {
        $filename = $this->cacheDir . md5($key) . '.txt';
        if (file_exists($filename)) {
            unlink($filename); // 强制删除文件
        }
    }
}